package com.rran.study.designpattern.behavioral.observer;

import com.rran.study.designpattern.behavioral.observer.event.PlaceOrderEvent;
import com.rran.study.designpattern.behavioral.observer.listener.simple.GenerateOrdersObserver;
import com.rran.study.designpattern.behavioral.observer.listener.simple.PayObserver;
import com.rran.study.designpattern.behavioral.observer.listener.simple.SendMsgObserver;
import com.rran.study.designpattern.behavioral.observer.model.OrderData;
import com.rran.study.designpattern.behavioral.observer.service.PlaceOrderTriggerSource;
import com.rran.study.designpattern.behavioral.observer.service.TriggerSource;

import java.math.BigDecimal;

/**
 * 简单的观察者模式
 */
public class SimpleObserverApplication {

    public static void main(String[] args) {

        TriggerSource placeOrderTriggerObserver = new PlaceOrderTriggerSource();
        //把观察者对象添加到启动观察者实例中，相当于订阅
        placeOrderTriggerObserver.registObserver(new GenerateOrdersObserver());
        placeOrderTriggerObserver.registObserver(new PayObserver());
        placeOrderTriggerObserver.registObserver(new SendMsgObserver());
        //实例化下单事件类，并且设置订单数据
        PlaceOrderEvent event = new PlaceOrderEvent(new OrderData(BigDecimal.valueOf(1000),20));
        //把下单事件传给所有的观察者，然后执行他们自己的业务逻辑，相当于发布
        placeOrderTriggerObserver.startUpObserver(event);
    }
}
